﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FanaticToy.Core.Utils
{
    ///<summary>路径工具集。</summary>
    internal class PathUtils
    {
        public static bool IsFileNameMatched(string name, IEnumerable<string> startWiths, IEnumerable<string> endWiths)
        {
            foreach (var startWith in startWiths)
            {
                if (name.StartsWith(startWith, StringComparison.OrdinalIgnoreCase)) return true;
            }

            foreach (var endWith in endWiths)
            {
                if (name.EndsWith(endWith, StringComparison.OrdinalIgnoreCase)) return true;
            }

            return false;
        }

        public static bool IsFileNameWithIdMatched(string name, string id, IEnumerable<string> startWiths, IEnumerable<string> endWiths)
        {
            foreach (var startWith in startWiths)
            {
                var combineName = startWith + id;
                if (string.Equals(name, combineName, StringComparison.OrdinalIgnoreCase)) return true;
            }

            foreach (var endWith in endWiths)
            {
                var combineName = id + endWith;
                if (string.Equals(name, combineName, StringComparison.OrdinalIgnoreCase)) return true;
            }

            foreach (var startWith in startWiths)
            {

                foreach (var endWith in endWiths)
                {
                    var combineName = startWith + id + endWith;
                    if (string.Equals(name, combineName, StringComparison.OrdinalIgnoreCase)) return true;
                }

            }

            return false;
        }

        ///<summary>获得冗余信息裁剪后的结果。</summary>
        public static string GetTrimedResult(string name, IEnumerable<string> startWiths, IEnumerable<string> endWiths)
        {
            var result = name;

            foreach (var startWith in startWiths)
            {
                if (name.StartsWith(startWith, StringComparison.OrdinalIgnoreCase))
                {
                    result = result.Substring(startWith.Length);
                    break;
                }
            }

            foreach (var endWith in endWiths)
            {
                if (name.EndsWith(endWith, StringComparison.OrdinalIgnoreCase))
                {
                    result = result.Substring(0, result.Length - endWith.Length);
                    break;
                }
            }

            return result;
        }

        public static bool IsPathEqual(string path1, string path2)
        {
            path1 = path1.Trim().Replace('\\','/');
            path2 = path2.Trim().Replace('\\', '/');
            return string.Equals(path1, path2, StringComparison.OrdinalIgnoreCase);
        }
    }
}
